package com.zz.hhbl.utils.location

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import com.amap.api.location.AMapLocation
import com.amap.api.location.AMapLocationClient
import com.amap.api.location.AMapLocationClientOption
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.StateFlow
import javax.inject.Inject

/**
 * Copyright © 2025 zhun All rights reserved.
 * Created by ZhaoZhun on 2025/2/24 17:44.
 * @author: ZhaoZhun 1820022519@qq.com
 * @version: V1.0
 */
@HiltViewModel
class LocationViewModel @Inject constructor(application: Application) : AndroidViewModel(application) {
    private val repository = LocationRepository(application)
    val locationState: StateFlow<LocationResult?> = repository.locationFlow

    fun startLocation(locationOption: AMapLocationClientOption = AMapLocationClientOption().apply {
        locationMode = AMapLocationClientOption.AMapLocationMode.Hight_Accuracy
        isOnceLocation = false
        interval = 30 * 60 * 1000L //30分钟一次
    }) {
        repository.startLocation(locationOption = locationOption)
    }

    fun stopLocation() {
        repository.stopLocation()
    }

    suspend fun getSingleLocation(): Result<LocationResult?> {
        return repository.getSingleLocation()
    }

    override fun onCleared() {
        super.onCleared()
        repository.release()
    }
}